<template>
  <pro-layout
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :i18nRender="i18nRender"
    v-bind="settings"
  >
    <template v-slot:menuHeaderRender>
      <div>
        <img src="@/assets/logo.png" />
        <h1>{{ title }}</h1>
      </div>
    </template>
    <!-- 1.0.0+ 版本 pro-layout 提供 API,
          增加 Header 左侧内容区自定义
    -->
    <template v-slot:headerContentRender>
      <div>
        <a-tooltip title="刷新页面">
          <a-icon type="reload" style="font-size: 18px;cursor: pointer;" @click="onIsShow" />
        </a-tooltip>
      </div>
    </template>

    <!-- <setting-drawer v-if="isDev" :settings="settings" @change="handleSettingChange">
      <div style="margin: 12px 0;">
        This is SettingDrawer custom footer content.
      </div>
    </setting-drawer> -->
    <template v-slot:rightContentRender>
      <right-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile" :theme="settings.theme" />
    </template>
    <!-- custom footer / 自定义Footer -->
    <template v-slot:footerRender>
      <!-- <global-footer /> -->
    </template>
    <div id="mask" v-if="!hasPassword" style="background-color: rgba(33, 33, 33, 0.5); width: 100%; height: 100vh; top: 0; left: 0; position: fixed; z-index: 999;">

      <a-card style="width: 500px; margin: 100px auto ;" :bordered="false">
        <!-- <div style="width: 320px;margin-top: 10px;">
          <label style="width: 80px;display: block;float: left;text-align: right;">账号ID</label>
          <a-input :disabled="true" style="width: 220px;margin-left: 10px;" v-model="formData.id"/>
        </div> -->
        <div class="account-settings-info-title">
          <h4 style="font-size:20px;text-align: center;">登录成功，设置密码</h4>
        </div>
        <div style="width: 350px;margin-top: 50px; height: 40px;">
          <label style="width: 120px;display: block;float: left;text-align: right;">登录账号</label>
          <a-input :disabled="true" style="width: 220px;margin-top: -10px; margin-left: 10px;" v-model="formData.mobile"/>
        </div>
        <!-- <div style="width: 350px;margin-top: 10px;">
          <label style="width: 120px;display: block;float: left;text-align: right;"><span style="color: red;">*</span>正在使用的密码</label>
          <a-input-password style="width: 220px;margin-left: 10px;" v-model="formData.pwd"/>
        </div> -->
        <div style="width: 350px;margin-top: 10px;height: 40px;">
          <label style="width: 120px;display: block;float: left;text-align: right;"><span style="color: red;">*</span>新密码</label>
          <a-input-password style="width: 220px;margin-left: 10px;" v-model="formData.newPwd"/>
        </div>
        <div style="width: 350px;margin-top: 10px;height: 40px;">
          <label style="width: 120px;display: block;float: left;text-align: right;"><span style="color: red;">*</span>确认密码</label>
          <a-input-password style="width: 220px;margin-left: 10px;" v-model="formData.confirm_password"/>
        </div>
        <div style="width: 350px;">
          <a-button style="margin-top: 20px; float: right;" @click="onCheckPasswordStrength" type="primary">确认</a-button>
        </div>
      </a-card>
    </div>
    <router-view v-if="isShow"/>
  </pro-layout>
</template>

<script>
import { updateTheme } from '@ant-design-vue/pro-layout'
import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'

import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'
import Ads from '@/components/Other/CarbonAds'
import { CheckPasswordStrength, ResetPassword } from '@/api/login'
import { message } from 'ant-design-vue'
export default {
  name: 'BasicLayout',
  components: {
    // SettingDrawer,
    RightContent,
    GlobalFooter,
    Ads
  },
  data () {
    return {
      isShow: true,
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end
      isDev: process.env.NODE_ENV === 'development' || process.env.VUE_APP_PREVIEW === 'true',

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: defaultSettings.title,
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,

        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false,
      formData: {
        mobile: '',
        pwd: '',
        newPwd: '',
        confirm_password: ''
      }
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters,
      hasPassword: state => state.user.hasPassword,
      info: state => state.user.info
    })
  },
  created () {
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
    this.formData.mobile = this.info.mobile
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    i18nRender,
    onResetPassword () {
      const self = this
      // 修改密码
      const form = {
        password: self.formData.newPwd,
        confirm_password: self.formData.confirm_password
      }
      ResetPassword(form).then((res) => {
        if (res.is_ok === 'Y') {
          self.formData = {}
          message.success('修改密码成功')
          this.$router.go(0)
          const loginInfo = JSON.parse(localStorage.getItem('loginInfo'))
          loginInfo.has_password = true
          localStorage.setItem('loginInfo', JSON.stringify(loginInfo))
        }
      })
    },
    onCheckPasswordStrength () {
      // 校验密码
      const self = this
      if (self.formData.newPwd === '') {
        message.warning('密码不能为空')
        return
      }
      if (self.formData.newPwd !== self.formData.confirm_password) {
        message.warning('两次密码不一致,请重新输入')
        return
      }
      // 校验密码等级
      CheckPasswordStrength({ password: self.formData.newPwd }).then((res) => {
        if (res !== null) {
          self.onResetPassword()
        }
      })
    },
    onIsShow () {
      this.isShow = false
      this.$nextTick(() => {
        this.isShow = true
      })
    },
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
        // this.settings.fixSiderbar = false
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      console.log('type', type, value)
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    }
  }
}
</script>

<style lang="less">
@import "./BasicLayout.less";
</style>
